﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class ViewInvoice : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
    
    List<_Article> document_article = new List<_Article>();
    string document_id;
    string user_doc;
    static decimal kopa_izsniegts;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx");
        }

        if (Request.IsAuthenticated)
        {
            string n = "";
            SqlCommand sCmd = new SqlCommand("SELECT name FROM USERS WHERE id=@id", conn);
            sCmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int)).Value = User.Identity.Name;
            conn.Open();
            SqlDataReader dr = sCmd.ExecuteReader();
            while (dr.Read())
            {
                string name = Convert.ToString(dr[0].ToString());
                n = name;
            }
            dr.Close();
            conn.Close();

            WelcomeBackMessage.Text = "Welcome back, " + n + "!";

            AuthenticatedHeaderPanel.Visible = true;
            AuthenticatedMessagePanel.Visible = true;
            AuthenticatedMenuPanel.Visible = true;

            if (!IsPostBack)
            {
                SetData();
            }
        }
        else
        {
            AuthenticatedHeaderPanel.Visible = false;
            AuthenticatedMessagePanel.Visible = false;
            AuthenticatedMenuPanel.Visible = false;
        }
    }

    public void SetData()
    {
        Response.Write(document_id = Session["Doc_ID"].ToString());
        Response.Write(user_doc = Session["user_doc"].ToString());

        lblDocumentID.Text = document_id;

        lbl_doc_num.Text = _Invoice.ViewInvoice(document_id).document_number;
        lbl_doc_date.Text = _Invoice.ViewInvoice(document_id).document_date.ToString("dd/MM/yyyy");
        lbl_payment_date.Text = _Invoice.ViewInvoice(document_id).document_payment_date.ToString("dd/MM/yyyy");

        lbl_company_by.Text = _Invoice.ViewInvoice(document_id).company_issued_by;
        lbl_company_by_reg.Text = _Invoice.ViewInvoice(document_id).company_issued_by_regnr;
        lbl_company_by_vat.Text = _Invoice.ViewInvoice(document_id).company_issued_by_vat;
        lbl_company_by_address.Text = _Invoice.ViewInvoice(document_id).address_company_by;
        lbl_company_by_bank.Text = _Invoice.ViewInvoice(document_id).bank_company_by;
        lbl_company_by_bank_swift.Text = _Invoice.ViewInvoice(document_id).bank_company_by_swift;
        lbl_company_by_account.Text = _Invoice.ViewInvoice(document_id).account_company_by;
        lbl_delivery_place.Text = _Invoice.ViewInvoice(document_id).place_of_delivery;

        lbl_client_to.Text = _Invoice.ViewInvoice(document_id).client_issued_to;
        lbl_client_to_reg.Text = _Invoice.ViewInvoice(document_id).client_issued_to_regnr;
        lbl_client_to_vat.Text = _Invoice.ViewInvoice(document_id).client_issued_to_vat;
        lbl_client_to_address.Text = _Invoice.ViewInvoice(document_id).address_client_to;
        lbl_client_to_bank.Text = _Invoice.ViewInvoice(document_id).bank_client_to;
        lbl_client_to_bank_swift.Text = _Invoice.ViewInvoice(document_id).bank_client_to_swift;
        lbl_client_to_account.Text = _Invoice.ViewInvoice(document_id).account_client_to;
        lbl_client_to_address2.Text = _Invoice.ViewInvoice(document_id).address_client_to;

        lbl_doc_type.Text = _Invoice.ViewInvoice(document_id).document_type;
        lbl_payment_type.Text = _Invoice.ViewInvoice(document_id).payment_type;
        lbl_user.Text = _Invoice.ViewInvoice(document_id).issued_by_user;
        lbl_doc_date2.Text = _Invoice.ViewInvoice(document_id).document_date.ToString("dd/MM/yyyy");

        Response.Clear();

        document_article = _Article.SlectArticle(document_id);
        article_list.DataSource = document_article;
        article_list.DataBind();

        if (user_doc == "user_doc")
        {
            lnkprintPDF.Visible = false;
        }
        else
        {
            lnkprintPDF.Visible = true;
        }
    }

    static List<decimal> list = new List<decimal>();
    
    protected void article_list_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        Label art_sum = (Label)e.Item.FindControl("sumLabel");
        decimal kopa = Convert.ToDecimal(art_sum.Text);
        list.Add(kopa);
        kopa_izsniegts = list.Sum();
    }

    //protected void bt_delete_invoice_Click(object sender, EventArgs e)
    //{
    //    //string document_ID = document_id;
    //    //_Article.Delete_all_articles(document_ID);
    //    ////_Invoice.Delete(document_ID);
    //    //Response.Redirect("Default.aspx");
    //}

    protected void article_list_PreRender1(object sender, EventArgs e)
    {
        if (document_article.Count == 0)
        {

        }
        else
        {
            Label lblTot = article_list.FindControl("lbl_kopa") as Label;
            lblTot.Text = kopa_izsniegts.ToString();

            Label lbl_PVN_likme = article_list.FindControl("lbl_PVN_likme") as Label;
            lbl_PVN_likme.Text = PVN+"%";

            decimal pvn1 = (decimal) (PVN/100.0);
            Label lbl_PVN = article_list.FindControl("lbl_PVN") as Label;
            decimal arPVN = (kopa_izsniegts * pvn1);
            lbl_PVN.Text = Convert.ToString(Math.Round(arPVN, 2));

            Label lbl_pavisam = article_list.FindControl("lbl_pavisam") as Label;
            decimal pavisam = kopa_izsniegts + arPVN;
            lbl_pavisam.Text = Convert.ToString(Math.Round(pavisam, 2));
            list.Clear();
        }
    }

    int PVN = Convert.ToInt32(ConfigurationManager.AppSettings["PVN"]);

    protected void lnkprintPDF_Click(object sender, ImageClickEventArgs e)
    {
        string id_document = document_id;
        Session["Doc_ID"] = id_document;
        
        string n = string.Format("{0:dd_MM_yyyy}", DateTime.Now);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + lbl_doc_num.Text + "_" + n + ".pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        
        StringWriter sw = new StringWriter();

        var document = new Document(PageSize.A4, 01, 01, 25, 25);

        // Izveido PDF turpat mapē
        //var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
        //var writer = PdfWriter.GetInstance(document, output);

        PdfWriter.GetInstance(document, Response.OutputStream);

        // Atver dokumentu lai ierakstītu
        document.Open();

        ////////////////////var welcomeParagraph = new Paragraph("Hello, World!");
        
        //Unicode Arial
        string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arialbd.TTF");
        
        //Create a base font object making sure to specify IDENTITY-H
        var bf = BaseFont.CreateFont(arialuniTff, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        var f = new Font(bf, 9, Font.NORMAL);
        var bold = new Font(bf, 9, Font.BOLD);
        var bold14 = new Font(bf, 12, Font.BOLD);

        #region COMPANY BY table

        var companyBy = new PdfPTable(4);
        companyBy.SpacingBefore = 3;
        float[] companyBywidths = { 15f, 25f, 10f, 25f };
        companyBy.SetWidths(companyBywidths);

        var docTipeNum = new PdfPCell(new Phrase(lbl_doc_type.Text + ": " + lbl_doc_num.Text, bold14));
        docTipeNum.FixedHeight = 35;
        docTipeNum.VerticalAlignment = Element.ALIGN_MIDDLE;
        docTipeNum.Colspan = 4;
        docTipeNum.Border=0;
        docTipeNum.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        companyBy.AddCell(docTipeNum);

        var docDate = new PdfPCell(new Phrase(lbl_doc_date.Text, f));
        docDate.Colspan = 4;
        docDate.Border = 0;
        docDate.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        companyBy.AddCell(docDate);
        
        var precnosutitajs = new PdfPCell(new Phrase("Preču nosūtītājs:", bold));
        precnosutitajs.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajs.BorderWidthBottom = 0;
        precnosutitajs.BorderWidthLeft = 0;
        precnosutitajs.BorderWidthRight = 0;
        precnosutitajs.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajs);

        var precnosutitajs1 = new PdfPCell(new Phrase(lbl_company_by.Text, f));
        precnosutitajs1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajs1.BorderWidthLeft = 0;
        precnosutitajs1.BorderWidthRight = 0;
        precnosutitajs1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajs1);

        var regnr = new PdfPCell(new Phrase("Reģ. Nr.:", bold));
        regnr.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        regnr.BorderWidthBottom = 0;
        regnr.BorderWidthLeft = 0;
        regnr.BorderWidthRight = 0;
        regnr.BorderWidthTop = 0;
        companyBy.AddCell(regnr);

        var regnr1 = new PdfPCell(new Phrase(lbl_company_by_reg.Text, f));
        regnr1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        regnr1.BorderWidthLeft = 0;
        regnr1.BorderWidthRight = 0;
        regnr1.BorderWidthTop = 0;
        companyBy.AddCell(regnr1);

        var precnosutitajsadrese = new PdfPCell(new Phrase("Adrese:", bold));
        precnosutitajsadrese.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajsadrese.BorderWidthBottom = 0;
        precnosutitajsadrese.BorderWidthLeft = 0;
        precnosutitajsadrese.BorderWidthRight = 0;
        precnosutitajsadrese.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsadrese);

        var precnosutitajsadrese1 = new PdfPCell(new Phrase(lbl_company_by_address.Text, f));
        //precnosutitajsadrese1.MinimumHeight = 50f;
        precnosutitajsadrese1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajsadrese1.BorderWidthLeft = 0;
        precnosutitajsadrese1.BorderWidthRight = 0;
        precnosutitajsadrese1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsadrese1);

        var PVNregnr = new PdfPCell(new Phrase("PVN Reģ. Nr.:", bold));
        PVNregnr.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        PVNregnr.BorderWidthBottom = 0;
        PVNregnr.BorderWidthLeft = 0;
        PVNregnr.BorderWidthRight = 0;
        PVNregnr.BorderWidthTop = 0;
        companyBy.AddCell(PVNregnr);

        var PVNregnr1 = new PdfPCell(new Phrase(lbl_company_by_vat.Text, f));
        PVNregnr1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        PVNregnr1.BorderWidthLeft = 0;
        PVNregnr1.BorderWidthRight = 0;
        PVNregnr1.BorderWidthTop = 0;
        companyBy.AddCell(PVNregnr1);


        var precnosutitajsrekviziti = new PdfPCell(new Phrase("Norēķinu rekvizīti:", bold));
        precnosutitajsrekviziti.Rowspan = 2;
        precnosutitajsrekviziti.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajsrekviziti.VerticalAlignment = Element.ALIGN_MIDDLE;
        precnosutitajsrekviziti.BorderWidthBottom = 0;
        precnosutitajsrekviziti.BorderWidthLeft = 0;
        precnosutitajsrekviziti.BorderWidthRight = 0;
        precnosutitajsrekviziti.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsrekviziti);

        var precnosutitajsrekviziti1 = new PdfPCell(new Phrase(lbl_company_by_bank.Text, f));
        precnosutitajsrekviziti1.Rowspan = 2;
        precnosutitajsrekviziti1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajsrekviziti1.VerticalAlignment = Element.ALIGN_MIDDLE;
        precnosutitajsrekviziti1.BorderWidthLeft = 0;
        precnosutitajsrekviziti1.BorderWidthRight = 0;
        precnosutitajsrekviziti1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsrekviziti1);

        var precnosutitajskonts = new PdfPCell(new Phrase("Konts:", bold));
        precnosutitajskonts.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajskonts.BorderWidthBottom = 0;
        precnosutitajskonts.BorderWidthLeft = 0;
        precnosutitajskonts.BorderWidthRight = 0;
        precnosutitajskonts.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajskonts);

        var precnosutitajskonts1 = new PdfPCell(new Phrase(lbl_company_by_account.Text, f));
        precnosutitajskonts1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajskonts1.BorderWidthLeft = 0;
        precnosutitajskonts1.BorderWidthRight = 0;
        precnosutitajskonts1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajskonts1);

        var precnosutitajskods = new PdfPCell(new Phrase("Kods:", bold));
        precnosutitajskods.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajskods.BorderWidthBottom = 0;
        precnosutitajskods.BorderWidthLeft = 0;
        precnosutitajskods.BorderWidthRight = 0;
        precnosutitajskods.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajskods);

        var precnosutitajskods1 = new PdfPCell(new Phrase(lbl_company_by_bank_swift.Text, f));
        precnosutitajskods1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajskods1.BorderWidthLeft = 0;
        precnosutitajskods1.BorderWidthRight = 0;
        precnosutitajskods1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajskods1);

        var precnosutitajsizsnvieta = new PdfPCell(new Phrase("Izsniegšanas vieta:", bold));
        precnosutitajsizsnvieta.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnosutitajsizsnvieta.BorderWidthBottom = 0;
        precnosutitajsizsnvieta.BorderWidthLeft = 0;
        precnosutitajsizsnvieta.BorderWidthRight = 0;
        precnosutitajsizsnvieta.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsizsnvieta);

        var precnosutitajsizsnvieta1 = new PdfPCell(new Phrase(lbl_client_to_address2.Text, f));
        precnosutitajsizsnvieta1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnosutitajsizsnvieta1.BorderWidthLeft = 0;
        precnosutitajsizsnvieta1.BorderWidthRight = 0;
        precnosutitajsizsnvieta1.BorderWidthTop = 0;
        companyBy.AddCell(precnosutitajsizsnvieta1);

        var empty = new PdfPCell(new Phrase(" ", f));
        empty.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        empty.BorderWidthBottom = 0;
        empty.BorderWidthLeft = 0;
        empty.BorderWidthRight = 0;
        empty.BorderWidthTop = 0;
        companyBy.AddCell(empty);

        var empty1 = new PdfPCell(new Phrase(" ", f));
        empty1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        empty1.BorderWidthBottom = 0;
        empty1.BorderWidthLeft = 0;
        empty1.BorderWidthRight = 0;
        empty1.BorderWidthTop = 0;
        companyBy.AddCell(empty1);

        #endregion

        var linija = new PdfPTable(4);
        linija.SpacingBefore = 3;
        var cell = new PdfPCell(new Phrase(" "));
        cell.Colspan = 4;
        cell.FixedHeight = 3;
        cell.BorderWidthLeft = 0;
        cell.BorderWidthRight = 0;
        linija.AddCell(cell);

        #region CLIENT TO table

        var clientTo = new PdfPTable(4);
        clientTo.SpacingBefore = 3;
        float[] clientTowidths = { 15f, 25f, 10f, 25f };
        clientTo.SetWidths(clientTowidths);

        var precnossanemejs = new PdfPCell(new Phrase("Preču saņēmējs:", bold));
        precnossanemejs.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        precnossanemejs.BorderWidthBottom = 0;
        precnossanemejs.BorderWidthLeft = 0;
        precnossanemejs.BorderWidthRight = 0;
        precnossanemejs.BorderWidthTop = 0;
        clientTo.AddCell(precnossanemejs);

        var precnossanemejs1 = new PdfPCell(new Phrase(lbl_client_to.Text, f));
        precnossanemejs1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        precnossanemejs1.BorderWidthLeft = 0;
        precnossanemejs1.BorderWidthRight = 0;
        precnossanemejs1.BorderWidthTop = 0;
        clientTo.AddCell(precnossanemejs1);

        var psregnr = new PdfPCell(new Phrase("Reģ. Nr.:", bold));
        psregnr.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        psregnr.BorderWidthBottom = 0;
        psregnr.BorderWidthLeft = 0;
        psregnr.BorderWidthRight = 0;
        psregnr.BorderWidthTop = 0;
        clientTo.AddCell(psregnr);

        var psregnr1 = new PdfPCell(new Phrase(lbl_client_to_reg.Text, f));
        psregnr1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psregnr1.BorderWidthLeft = 0;
        psregnr1.BorderWidthRight = 0;
        psregnr1.BorderWidthTop = 0;
        clientTo.AddCell(psregnr1);

        var psadrese = new PdfPCell(new Phrase("Adrese:", bold));
        psadrese.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        psadrese.BorderWidthBottom = 0;
        psadrese.BorderWidthLeft = 0;
        psadrese.BorderWidthRight = 0;
        psadrese.BorderWidthTop = 0;
        clientTo.AddCell(psadrese);

        var psadrese1 = new PdfPCell(new Phrase(lbl_client_to_address.Text, f));
        //psadrese1.MinimumHeight = 130f;
        psadrese1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psadrese1.BorderWidthLeft = 0;
        psadrese1.BorderWidthRight = 0;
        psadrese1.BorderWidthTop = 0;
        clientTo.AddCell(psadrese1);

        var psPVNregnr = new PdfPCell(new Phrase("PVN Reģ. Nr.:", bold));
        psPVNregnr.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        psPVNregnr.BorderWidthBottom = 0;
        psPVNregnr.BorderWidthLeft = 0;
        psPVNregnr.BorderWidthRight = 0;
        psPVNregnr.BorderWidthTop = 0;
        clientTo.AddCell(psPVNregnr);

        var psPVNregnr1 = new PdfPCell(new Phrase(lbl_client_to_vat.Text, f));
        psPVNregnr1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psPVNregnr1.BorderWidthLeft = 0;
        psPVNregnr1.BorderWidthRight = 0;
        psPVNregnr1.BorderWidthTop = 0;
        clientTo.AddCell(psPVNregnr1);

        var psrekviziti = new PdfPCell(new Phrase("Norēķinu rekvizīti:", bold));
        psrekviziti.Rowspan = 2;
        psrekviziti.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        psrekviziti.VerticalAlignment = Element.ALIGN_MIDDLE;
        psrekviziti.BorderWidthBottom = 0;
        psrekviziti.BorderWidthLeft = 0;
        psrekviziti.BorderWidthRight = 0;
        psrekviziti.BorderWidthTop = 0;
        clientTo.AddCell(psrekviziti);

        var psrekviziti1 = new PdfPCell(new Phrase(lbl_client_to_bank.Text, f));
        psrekviziti1.Rowspan = 2;
        psrekviziti1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psrekviziti1.VerticalAlignment = Element.ALIGN_MIDDLE;
        psrekviziti1.BorderWidthLeft = 0;
        psrekviziti1.BorderWidthRight = 0;
        psrekviziti1.BorderWidthTop = 0;
        clientTo.AddCell(psrekviziti1);

        var pskonts = new PdfPCell(new Phrase("Konts:", bold));
        pskonts.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        pskonts.BorderWidthBottom = 0;
        pskonts.BorderWidthLeft = 0;
        pskonts.BorderWidthRight = 0;
        pskonts.BorderWidthTop = 0;
        clientTo.AddCell(pskonts);

        var pskonts1 = new PdfPCell(new Phrase(lbl_client_to_account.Text, f));
        pskonts1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        pskonts1.BorderWidthLeft = 0;
        pskonts1.BorderWidthRight = 0;
        pskonts1.BorderWidthTop = 0;
        clientTo.AddCell(pskonts1);

        var pskods = new PdfPCell(new Phrase("Kods:", bold));
        pskods.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        pskods.BorderWidthBottom = 0;
        pskods.BorderWidthLeft = 0;
        pskods.BorderWidthRight = 0;
        pskods.BorderWidthTop = 0;
        clientTo.AddCell(pskods);

        var pskods1 = new PdfPCell(new Phrase(lbl_client_to_bank_swift.Text, f));
        pskods1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        pskods1.BorderWidthLeft = 0;
        pskods1.BorderWidthRight = 0;
        pskods1.BorderWidthTop = 0;
        clientTo.AddCell(pskods1);

        var pspiegvieta = new PdfPCell(new Phrase("Piegādes vieta:", bold));
        pspiegvieta.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        pspiegvieta.BorderWidthBottom = 0;
        pspiegvieta.BorderWidthLeft = 0;
        pspiegvieta.BorderWidthRight = 0;
        pspiegvieta.BorderWidthTop = 0;
        clientTo.AddCell(pspiegvieta);

        var pspiegvieta1 = new PdfPCell(new Phrase(lbl_delivery_place.Text, f));
        pspiegvieta1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        pspiegvieta1.BorderWidthLeft = 0;
        pspiegvieta1.BorderWidthRight = 0;
        pspiegvieta1.BorderWidthTop = 0;
        clientTo.AddCell(pspiegvieta1);

        var psempty = new PdfPCell(new Phrase(" ", f));
        psempty.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psempty.BorderWidthBottom = 0;
        psempty.BorderWidthLeft = 0;
        psempty.BorderWidthRight = 0;
        psempty.BorderWidthTop = 0;
        clientTo.AddCell(psempty);

        var psempty1 = new PdfPCell(new Phrase(" ", f));
        psempty1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        psempty1.BorderWidthBottom = 0;
        psempty1.BorderWidthLeft = 0;
        psempty1.BorderWidthRight = 0;
        psempty1.BorderWidthTop = 0;
        clientTo.AddCell(psempty1);

        #endregion

        var linija1 = new PdfPTable(4);
        linija1.SpacingBefore = 3;
        var cell1 = new PdfPCell(new Phrase(" "));
        cell1.Colspan = 4;
        cell1.FixedHeight = 3;
        cell1.BorderWidthLeft = 0;
        cell1.BorderWidthRight = 0;
        linija1.AddCell(cell1);

        #region SAMAKSA table

        var samaksa = new PdfPTable(4);
        samaksa.SpacingBefore = 3;
        float[] samaksawidths = { 15f, 25f, 10f, 25f };
        samaksa.SetWidths(samaksawidths);
        var samaksasveids = new PdfPCell(new Phrase("Samaksas veids:", bold));
        samaksasveids.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        samaksasveids.BorderWidthBottom = 0;
        samaksasveids.BorderWidthLeft = 0;
        samaksasveids.BorderWidthRight = 0;
        samaksasveids.BorderWidthTop = 0;
        samaksa.AddCell(samaksasveids);

        var samaksasveids1 = new PdfPCell(new Phrase(lbl_payment_type.Text, f));
        samaksasveids1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        samaksasveids1.BorderWidthLeft = 0;
        samaksasveids1.BorderWidthRight = 0;
        samaksasveids1.BorderWidthTop = 0;
        samaksa.AddCell(samaksasveids1);

        var empty3 = new PdfPCell(new Phrase(" "));
        empty3.BorderWidthBottom = 0;
        empty3.BorderWidthLeft = 0;
        empty3.BorderWidthRight = 0;
        empty3.BorderWidthTop = 0;
        samaksa.AddCell(empty3);

        var empty4 = new PdfPCell(new Phrase(" "));
        empty4.BorderWidthBottom = 0;
        empty4.BorderWidthLeft = 0;
        empty4.BorderWidthRight = 0;
        empty4.BorderWidthTop = 0;
        samaksa.AddCell(empty4);

        var samaksatlidz = new PdfPCell(new Phrase("Samaksat līdz:", bold));
        samaksatlidz.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        samaksatlidz.BorderWidthBottom = 0;
        samaksatlidz.BorderWidthLeft = 0;
        samaksatlidz.BorderWidthRight = 0;
        samaksatlidz.BorderWidthTop = 0;
        samaksa.AddCell(samaksatlidz);

        var samaksatlidz1 = new PdfPCell(new Phrase(lbl_payment_date.Text, f));
        samaksatlidz1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        samaksatlidz1.BorderWidthLeft = 0;
        samaksatlidz1.BorderWidthRight = 0;
        samaksatlidz1.BorderWidthTop = 0;
        samaksa.AddCell(samaksatlidz1);

        var empty5 = new PdfPCell(new Phrase(" "));
        empty5.BorderWidthBottom = 0;
        empty5.BorderWidthLeft = 0;
        empty5.BorderWidthRight = 0;
        empty5.BorderWidthTop = 0;
        samaksa.AddCell(empty5);

        var empty6 = new PdfPCell(new Phrase(" "));
        empty6.BorderWidthBottom = 0;
        empty6.BorderWidthLeft = 0;
        empty6.BorderWidthRight = 0;
        empty6.BorderWidthTop = 0;
        samaksa.AddCell(empty6);
#endregion

        Label lblTot = article_list.FindControl("lbl_kopa") as Label;
        Label lbl_PVN = article_list.FindControl("lbl_PVN") as Label;
        Label lbl_pavisam = article_list.FindControl("lbl_pavisam") as Label;

        #region ARTICLE table

        var article = new PdfPTable(5);
        article.SpacingBefore = 3;
        float[] widths = {40f, 15f, 15f, 15f, 15f};
        article.SetWidths(widths);

        var cellarticle = new PdfPCell(new Phrase("Nosaukums", bold));
        cellarticle.HorizontalAlignment = 1;
        article.AddCell(cellarticle);
        var cellarticle1 = new PdfPCell(new Phrase("Vienība", bold));
        cellarticle1.HorizontalAlignment = 1;
        article.AddCell(cellarticle1);
        var cellarticle2 = new PdfPCell(new Phrase("Skaits", bold));
        cellarticle2.HorizontalAlignment = 1;
        article.AddCell(cellarticle2);
        var cellarticle3 = new PdfPCell(new Phrase("Cena, Ls", bold));
        cellarticle3.HorizontalAlignment = 1;
        article.AddCell(cellarticle3);
        var cellarticle4 = new PdfPCell(new Phrase("Summa, Ls", bold));
        cellarticle4.HorizontalAlignment = 1;
        article.AddCell(cellarticle4);

        document_article = _Article.SlectArticle(lblDocumentID.Text);
        foreach (var row in document_article)
        {
            var cellarticle5 = new PdfPCell(new Phrase(row._article_name, f));
            cellarticle5.HorizontalAlignment = 0;
            article.AddCell(cellarticle5);
            var cellarticle6 = new PdfPCell(new Phrase("gab.", f));
            cellarticle6.HorizontalAlignment = 1;
            article.AddCell(cellarticle6);
            var cellarticle7 = new PdfPCell(new Phrase(row._article_count.ToString(CultureInfo.InvariantCulture), f));
            cellarticle7.HorizontalAlignment = 1;
            article.AddCell(cellarticle7);
            var cellarticle8 = new PdfPCell(new Phrase(row._article_price.ToString(CultureInfo.InvariantCulture), f));
            cellarticle8.HorizontalAlignment = 1;
            article.AddCell(cellarticle8);
            var cellarticle9 = new PdfPCell(new Phrase(row._article_sum.ToString(CultureInfo.InvariantCulture), f));
            cellarticle9.HorizontalAlignment = 1;
            article.AddCell(cellarticle9);
        }

        var art1 = new PdfPCell(new Phrase("Kopā izsniegts:", bold));
        art1.HorizontalAlignment = 2;
        art1.Colspan = 3;
        art1.BorderWidthLeft = 0;
        art1.BorderWidthBottom = 0;
        art1.BorderWidthTop = 0;
        art1.BorderWidthRight = 0;
        article.AddCell(art1);
        var art2 = new PdfPCell(new Phrase(" ", f));
        art2.HorizontalAlignment = 1;
        article.AddCell(art2);
        var art3 = new PdfPCell(new Phrase(lblTot.Text, f));
        art3.HorizontalAlignment = 1;
        article.AddCell(art3);

        var art4 = new PdfPCell(new Phrase("Pievienotās vērtības nodoklis:", bold));
        art4.HorizontalAlignment = 2;
        art4.Colspan = 3;
        art4.BorderWidthLeft = 0;
        art4.BorderWidthBottom = 0;
        art4.BorderWidthTop = 0;
        art4.BorderWidthRight = 0;
        article.AddCell(art4);
        var art5 = new PdfPCell(new Phrase(PVN + "%", f));
        art5.HorizontalAlignment = 1;
        article.AddCell(art5);
        var art6 = new PdfPCell(new Phrase(lbl_PVN.Text, f));
        art6.HorizontalAlignment = 1;
        article.AddCell(art6);

        var art7 = new PdfPCell(new Phrase("Pavisam apmaksai:", bold));
        art7.HorizontalAlignment = 2;
        art7.Colspan = 3;
        art7.BorderWidthLeft = 0;
        art7.BorderWidthBottom = 0;
        art7.BorderWidthTop = 0;
        art7.BorderWidthRight = 0;
        article.AddCell(art7);
        var art8 = new PdfPCell(new Phrase("(ar cipariem)", f));
        art8.HorizontalAlignment = 1;
        article.AddCell(art8);

        var art9 = new PdfPCell(new Phrase(lbl_pavisam.Text, f));
        art9.HorizontalAlignment = 1;
        article.AddCell(art9);
#endregion

        var linija2 = new PdfPTable(4);
        linija2.SpacingBefore = 3;
        var cell2 = new PdfPCell(new Phrase(" "));
        cell2.Colspan = 4;
        cell2.BorderWidthLeft = 0;
        cell2.BorderWidthRight = 0;
        cell2.BorderWidthTop = 0;
        linija2.AddCell(cell2);

        #region INFO table

        var info = new PdfPTable(4);
        info.SpacingBefore = 3;
        float[] infowidths = { 10f, 25f, 10f, 25f };
        info.SetWidths(infowidths);

        var izsniedza = new PdfPCell(new Phrase("Izsniedza:", bold));
        izsniedza.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        izsniedza.BorderWidthBottom = 0;
        izsniedza.BorderWidthLeft = 0;
        izsniedza.BorderWidthRight = 0;
        izsniedza.BorderWidthTop = 0;
        info.AddCell(izsniedza);

        var izsniedza1 = new PdfPCell(new Phrase(lbl_user.Text, f));
        izsniedza1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        izsniedza1.BorderWidthLeft = 0;
        izsniedza1.BorderWidthRight = 0;
        izsniedza1.BorderWidthTop = 0;
        info.AddCell(izsniedza1);

        var pienema = new PdfPCell(new Phrase("Pieņēma:", bold));
        pienema.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        pienema.BorderWidthBottom = 0;
        pienema.BorderWidthLeft = 0;
        pienema.BorderWidthRight = 0;
        pienema.BorderWidthTop = 0;
        info.AddCell(pienema);

        var pienema1 = new PdfPCell(new Phrase("", f));
        pienema1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        pienema1.BorderWidthLeft = 0;
        pienema1.BorderWidthRight = 0;
        pienema1.BorderWidthTop = 0;
        info.AddCell(pienema1);

        var datumsizsn = new PdfPCell(new Phrase("Datums:", bold));
        datumsizsn.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        datumsizsn.BorderWidthBottom = 0;
        datumsizsn.BorderWidthLeft = 0;
        datumsizsn.BorderWidthRight = 0;
        datumsizsn.BorderWidthTop = 0;
        info.AddCell(datumsizsn);

        var datumsizsn1 = new PdfPCell(new Phrase(lbl_doc_date2.Text, f));
        datumsizsn1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        datumsizsn1.BorderWidthLeft = 0;
        datumsizsn1.BorderWidthRight = 0;
        datumsizsn1.BorderWidthTop = 0;
        info.AddCell(datumsizsn1);

        var datumspien = new PdfPCell(new Phrase("Datums:", bold));
        datumspien.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        datumspien.BorderWidthBottom = 0;
        datumspien.BorderWidthLeft = 0;
        datumspien.BorderWidthRight = 0;
        datumspien.BorderWidthTop = 0;
        info.AddCell(datumspien);

        var datumspien1 = new PdfPCell(new Phrase("", f));
        datumspien1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        datumspien1.BorderWidthLeft = 0;
        datumspien1.BorderWidthRight = 0;
        datumspien1.BorderWidthTop = 0;
        info.AddCell(datumspien1);
        
        var parakstsizsn = new PdfPCell(new Phrase("Paraksts:", bold));
        parakstsizsn.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        parakstsizsn.BorderWidthBottom = 0;
        parakstsizsn.BorderWidthLeft = 0;
        parakstsizsn.BorderWidthRight = 0;
        parakstsizsn.BorderWidthTop = 0;
        info.AddCell(parakstsizsn);

        var parakstsizsn1 = new PdfPCell(new Phrase("", f));
        parakstsizsn1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        parakstsizsn1.BorderWidthLeft = 0;
        parakstsizsn1.BorderWidthRight = 0;
        parakstsizsn1.BorderWidthTop = 0;
        info.AddCell(parakstsizsn1);

        var parakstssan = new PdfPCell(new Phrase("Paraksts:", bold));
        parakstssan.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
        parakstssan.BorderWidthBottom = 0;
        parakstssan.BorderWidthLeft = 0;
        parakstssan.BorderWidthRight = 0;
        parakstssan.BorderWidthTop = 0;
        info.AddCell(parakstssan);

        var parakstssan1 = new PdfPCell(new Phrase("", f));
        parakstssan1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
        parakstssan1.BorderWidthLeft = 0;
        parakstssan1.BorderWidthRight = 0;
        parakstssan1.BorderWidthTop = 0;
        info.AddCell(parakstssan1);

        #endregion

        document.Add(companyBy);
        document.Add(linija);
        document.Add(clientTo);
        document.Add(linija1);
        document.Add(samaksa);
        document.Add(article);
        document.Add(linija2);
        document.Add(info);

        document.Close();
        Response.Write(document);
        Response.End();

        // Izvada un atver uz ekrāna jaunā logā pdf...
        //string path = output.Name.ToString();
        //WebClient client = new WebClient();
        //Byte[] buffer = client.DownloadData(path);

        //if (buffer != null)
        //{
        //    Response.ContentType = "application/pdf";
        //    Response.AddHeader("content-length", buffer.Length.ToString());
        //    Response.BinaryWrite(buffer);
        //}
    }

/*
    public static string NumberToWords(int number)
    {
        if (number == 0)
            return "nulle";

        if (number < 0)
            return "mīnus " + NumberToWords(Math.Abs(number));

        string words = "";

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " milioni ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            if ((number/1000)== 0)
            {
                words += NumberToWords(number/1000) + " tūkstotis ";
                number %= 1000;
            }
            else
            {
                words += NumberToWords(number / 1000) + " tūkstoši ";
                number %= 1000;
            }
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " simti ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += "un ";

            var unitsMap = new[] { "nulle", "viens", "divi", "trīs", "četri", "pieci", "seši", "septiņi", "astoņi", "deviņi", "desmit", "vienpadsmit", "divpadsmit", "trīspadsmit", "četripadsmit", "piedspadsmit", "sešpadsmit", "septiņpadsmit", "astoņpadsmit", "deviņpadsmit" };
            var tensMap = new[] { "nulle", "desmit", "divdesmit", "trīsdesmit", "četridesmit", "piecdesmit", "sešdesmit", "septiņdesmit", "astoņdesmit", "deviņdesmit" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "" + unitsMap[number % 10];
            }
        }

        return words;
    }
 */
}